/*
 * @Author: NtyAaron
 * @Date: 2021-04-10 16:11:54
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-02 19:28:18
 * @Description: file content
 * @FilePath: \miniapp\dwq\pages\weatherForecast\weatherForecast.js
 */
import { getCityWeather } from '../../api/pubService/pubService'
import { getCity, getDefCity, getLangCityName } from '../../utils/cityJson'
import { _t } from '../../i18n/base'
import { getTqDesc } from '../../i18n/tq'
var citys
Page({
  data: {
    cityWeatherInfo: {},
    show: false,
    columnsCity: [],
    language: '',
    change: '',
  },
  // 获取天气
  async getCityWeather(e) {
    let weatherInfo = wx.getStorageSync('cityWeather')
    if (weatherInfo) {
      let weather = JSON.parse(weatherInfo).future.map((item) => {
        let index = item.date.indexOf('-')
        let time = item.date.substring(index + 1)
        item.weather = getTqDesc(item.wid)
        return {
          ...item,
          date: time,
        }
      })
      this.setData({
        ['cityWeatherInfo.city']: JSON.parse(weatherInfo).city,
        ['cityWeatherInfo.future']: weather,
      })
    }
    if (e) {
      let { result } = await getCityWeather(e)
      if (result.result == null) {
        console.log(2)
        wx.showToast({
          title: this.data.lang['该城市暂无数据'],
          icon: 'none',
        })
      } else {
        let obj = result.result
        let list = obj.future.map((item) => {
          let index = item.date.indexOf('-')
          let time = item.date.substring(index + 1)
          item.weather = getTqDesc(item.wid)
          return {
            ...item,
            date: time,
          }
        })
        this.setData({
          cityWeatherInfo: result.result,
          ['cityWeatherInfo.city']: getLangCityName(
            result.result.city,
            1,
            this.data.language
          ),
          ['cityWeatherInfo.future']: list,
        })
      }
    }
  },
  // 打开城市选择
  popupOpenHandle() {
    this.setData({
      show: true,
    })
  },
  // 关闭城市弹窗
  popupCloseHandle() {
    this.setData({
      show: false,
    })
  },
  // 取消城市选择
  cancelCityHandle() {
    this.setData({
      show: false,
    })
  },
  // 选择城市
  changeCityHandle({ detail }) {
    const { picker, value, index } = detail
    picker.setColumnValues(1, citys[value[0]])
  },
  // 确认城市
  confirmCityHandle({ detail }) {
    this.setData({
      ['cityWeatherInfo.city']: detail.value[1],
      show: false,
    })
    this.getCityWeather(
      getLangCityName(this.data.cityWeatherInfo.city, this.data.language, 1)
    )
  },
  // 复制网址
  copyHandle(e) {
    //香港国际
    let ch = {
      zh: 'http://www.weather.com.cn/',
      tw: '',
      en: 'http://en.weather.com.cn/',
    }
    //澳门国际
    let go = {
      zh: 'https://www.gbaweather.net/sc/',
      tw: 'https://www.gbaweather.net/tc/',
      en: 'https://www.gbaweather.net/en/',
    }
    let type = e.currentTarget.dataset.type
    let language = wx.getStorageSync('language')
    let url = ''
    if (type == 1) {
      url = ch[language]
    } else if (type == 2) {
      url = go[language]
    }
    if (url == undefined) {
      wx.showToast({
        title: this.data.lang['当前没有该网址'],
        icon: 'none',
      })
    }
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  onLoad(e) {
    let language = wx.getStorageSync('lang')
    citys = getCity()
    this.setData({
      columnsCity: [
        {
          values: Object.keys(citys),
        },
        {
          values: citys[getDefCity()],
          defaultIndex: 1,
        },
      ],
      lang: _t(),
      language,
      isOverseas: JSON.parse(e.isOverseas),
    })
    wx.setNavigationBarTitle({
      title: this.data.lang['天气预报'],
    })
    this.getCityWeather()
  },
})
